package com.example.springsecurity.handler;

import com.example.springsecurity.pojo.R;
import com.example.springsecurity.util.JwtUtils;
import com.example.springsecurity.util.ResponseUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * {@code @Author:} anwenbiao
 * {@code @Description:} TODO
 * {@code @DateTime:} 2023/2/5 11:55
 */
@Component
public class SuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        String jwt = JwtUtils.createJwt(String.valueOf(authentication.getPrincipal()));
        Map<String, String> map = new HashMap<>();
        map.put("token", jwt);
        ResponseUtils.sendWirte(response, R.success("认证成功",map));
    }
}
